using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using WODPL.Models;


namespace WODPL.GameComponents
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class GameMenu : DrawableGameComponent
    {
        private readonly GameComponentCollection menuItems = new GameComponentCollection();
        private KeyboardState prevState;        private SelectionModel selectionModel;

        public GameMenu(Game game, IEnumerable<GameMenuItemModel> models, SelectionModel selectionModel)
            : base(game)
        {
            this.selectionModel = selectionModel;
            SpriteFont spriteFont = Game.Content.Load<SpriteFont>("Fonts/MenuFont");
            Vector2 position = new Vector2(0.5f, 0.3f);
            foreach (var model in models)
            {
                position = position +
                           new Vector2(0, spriteFont.MeasureString(model.Text).Y/Game.GraphicsDevice.Viewport.Width);
                menuItems.Add(new GameMenuItem(game, model, selectionModel, position, spriteFont));
            }
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
            UpdateKeyboard();
            foreach (var item in menuItems)
            {
                var updateable = item as IUpdateable;
                if (updateable != null)
                {
                    updateable.Update(gameTime);
                }
            }
        }

        private void UpdateKeyboard()
        {
            var state = Keyboard.GetState();
            if (prevState == null)
            {
                prevState = state;
                return;
            }
            if (state.IsKeyDown(Keys.Up) && prevState.IsKeyUp(Keys.Up) ||
                state.IsKeyDown(Keys.Down) && prevState.IsKeyUp(Keys.Down))
            {
                if (state.IsKeyDown(Keys.Down))
                {
                    selectionModel++;
                }
                else
                {
                    selectionModel--;
                }
            }
            prevState = state;
        }

        public override void Draw(GameTime gameTime)
        {
            base.Draw(gameTime);
            GraphicsDevice.Clear(Color.Black);
            foreach (var item in menuItems)
            {
                var drawable = item as IDrawable;
                if (drawable != null)
                {
                    drawable.Draw(gameTime);
                }
            }
        }

        public override void Initialize()
        {
            base.Initialize();
            foreach (var item in menuItems)
            {
                item.Initialize();
            }
        }
    }
}